<template>
	<pd-popup type='dialog' ref='popup' :isMaskClick='false'>
		<view class="__privacy_read">
			<view class="title">
				为了使用小程序的完整功能，请您阅读并同意<text class="primary-color" @tap='viewAgreement'>{{ privacyContractName }}</text>
			</view>
			<button type='primary' open-type="agreePrivacyAuthorization" @agreeprivacyauthorization="agreeHandle"
				style="font-size: 30rpx;margin-top: 32rpx;">
				我已阅读并同意
			</button>
		</view>
	</pd-popup>

</template>

<script lang="ts" setup>
import PD from '@/pd-libs/libs';
import PdPopup from './pd-popup.vue';
import { WX_API_VERSION, type GetPrivacySettingRes } from '../libs/wx-types';
const popup = ref<InstanceType<typeof PdPopup>>();
const privacyContractName = ref('');

interface EmitType {
	(e: 'success'): void;
}
const emits = defineEmits<EmitType>();

onLoad(() => {
	if (PD.WxHelp.checkWxApiVersion(WX_API_VERSION.getPrivacySetting)) {
		// eslint-disable-next-line @typescript-eslint/no-explicit-any
		(wx as any).getPrivacySetting({
			success: (res: GetPrivacySettingRes) => {
				privacyContractName.value = res.privacyContractName;
				res.needAuthorization && popup.value?.open();
				if (res.needAuthorization) {
					popup.value?.open();
				} else {
					emits('success');
				};
			}
		});
	} else {
		emits('success');
	};
});

const agreeHandle = () => {
	popup.value?.close();
	emits('success');
};

//阻止默认的隐私协议弹窗
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(wx as any).onNeedPrivacyAuthorization(() => {
	console.log();
});

const viewAgreement = () => {
	// eslint-disable-next-line @typescript-eslint/no-explicit-any
	(wx as any).openPrivacyContract();
};
</script>

<style lang="less" scoped>
.__privacy_read {
	padding: 32rpx 50rpx;
	width: 600rpx;
	background: #fff;
	border-radius: 8rpx;
	font-size: 32rpx;

	button {
		margin-top: 40rpx;
	}
}
</style>